package com.wordpress.minhnvt1;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wordpress.minhnvt1.hrm.dba.DBAdapter;

public class RegisterForm extends Activity {
	
	Button btnReset;
	Button btnRegister;
	
	EditText txtName;
	EditText txtPass;
	EditText txtPassRe;
	EditText txtEmail;
	
	DBAdapter _db;
	Cursor _cursor;
	String _hrId;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registerform);
        btnReset = (Button)findViewById(R.id.btnRegReset);
        btnRegister = (Button)findViewById(R.id.btnRegRegister);
        
        txtName = (EditText)findViewById(R.id.etRegName);
        txtPass = (EditText)findViewById(R.id.etRegPass);
        txtPassRe = (EditText)findViewById(R.id.etRegPassRe);
        txtEmail = (EditText)findViewById(R.id.etRegEmail);
        
        
        btnReset.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				txtName.setText("");
				txtPass.setText("");
				txtPassRe.setText("");
				txtEmail.setText("");
			}
		});
        
        btnRegister.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String name = txtName.getText().toString();
				String pass = txtPass.getText().toString();
				String passre = txtPassRe.getText().toString();
				String email = txtEmail.getText().toString();
				
				if (pass.compareTo(passre) != 0) {
					Toast.makeText(getApplicationContext(), "Your password is not matched!.", Toast.LENGTH_SHORT).show();
				} else {
					_db = new DBAdapter(getApplicationContext());
					_db.openDB();
					
					_db.insertHumanResource(name, pass, email);
					
					Cursor c = _db.getHumanResourceByName(name);
					
					_hrId = c.getString(0).toString();
					
					Intent intent = new Intent(getApplicationContext(), ManagementForm.class);
					intent.putExtra("hrId", _hrId);
					startActivity(intent);
					_db.closeDB();
				}
			}
		});
	}
}
